package com.example.sqlite.adapters;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.example.sqlite.activities.R;
import com.example.sqlite.models.DetailInfo;
import com.example.sqlite.models.HeaderObject;

public class MyListAdapter extends BaseExpandableListAdapter {

	private final Context context;
	private final ArrayList<HeaderObject> objList;

	public MyListAdapter(Context context, ArrayList<HeaderObject> deptList) {
		this.context = context;
		this.objList = deptList;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		ArrayList<DetailInfo> productList = objList.get(groupPosition)
				.getAtributosList();
		return productList.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@SuppressLint("DefaultLocale")
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View view, ViewGroup parent) {

		DetailInfo detailInfo = (DetailInfo) getChild(groupPosition,
				childPosition);
		if (view == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = infalInflater.inflate(R.layout.child_row, null);
		}

		TextView sequence = (TextView) view.findViewById(R.id.sequence);
		String text = detailInfo.getAtributo().trim().toLowerCase();
		sequence.setText((text).substring(0, 1).toUpperCase()
				+ text.substring(1) + " : ");
		TextView childItem = (TextView) view.findViewById(R.id.childItem);
		childItem.setText(detailInfo.getValue().trim());

		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {

		ArrayList<DetailInfo> productList = objList.get(groupPosition)
				.getAtributosList();
		return productList.size();

	}

	@Override
	public Object getGroup(int groupPosition) {
		return objList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return objList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isLastChild, View view,
			ViewGroup parent) {

		HeaderObject headerInfo = (HeaderObject) getGroup(groupPosition);
		if (view == null) {
			LayoutInflater inf = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inf.inflate(R.layout.group_heading, null);
		}

		TextView heading = (TextView) view.findViewById(R.id.heading);
		heading.setText(headerInfo.getObjName().trim());

		return view;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}